﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Spreadsheet.UI
{
   
    class ControlHelper
    {




        public static bool GetAutoSelect(DependencyObject obj)
        {
            return (bool)obj.GetValue(AutoSelectProperty);
        }

        public static void SetAutoSelect(DependencyObject obj, bool value)
        {
            obj.SetValue(AutoSelectProperty, value);
        }

        // Using a DependencyProperty as the backing store for AutoSelect.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AutoSelectProperty =
            DependencyProperty.RegisterAttached("AutoSelect", typeof(bool), typeof(ControlHelper), 
            new UIPropertyMetadata(false, OnAutoSelectChanged));

        private static void OnAutoSelectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBox element = (TextBox)d;
            if (element.IsLoaded)
            {
                element.Focus();
                element.SelectAll();
            }
            else
            {
                element.Loaded += (RoutedEventHandler)(delegate { element.SelectAll();
                                                                    element.Focus(); });
            }
        }
    }
}